package com.meiqiongproperty.yiguanjia.fragment.home.health;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.databinding.YiHealthyMedicalLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendAdvertId;
import com.meiqiongproperty.yiguanjia.httpbean.AdvertInfoBean;
import com.meiqiongproperty.yiguanjia.ui.adbanner.animations.DescriptionAnimation;
import com.meiqiongproperty.yiguanjia.ui.adbanner.indicators.PagerIndicator;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.BaseSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.TextSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.viewpagerswitchanimation.SliderLayout;
import com.meiqiongproperty.yiguanjia.ui.dialog.PersonalCenterAlertDialog;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/21.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.health
 */

public class YiHealthFragment extends BaseFragment {
    YiHealthyMedicalLayoutBinding binding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.yi_healthy_medical_layout, container, false);
        binding.setClick(this);
        binding.navigation.setTitle("怡医疗");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        getData();
        return binding.getRoot();
    }

    public void publicBenefitClick(View view) {
        GenericFragmentActivity.start(getActivity(), PhysicalExaminationFragment.class, new Bundle());
    }


    public void registrationAppointmentClick(View view) {
        // GenericFragmentActivity.start(getActivity(),HealthyInfoFragment.class,new Bundle());
//        Toast.makeText(getActivity(),"功能暂未开放",Toast.LENGTH_SHORT).show();

        PersonalCenterAlertDialog dialog = new PersonalCenterAlertDialog(getActivity());
        dialog.builder().setCanceledOnTouchOutside(false).setMsg("挂号预约功能暂未开放")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }).show();
    }

    public void physicianConsultClick(View view) {
        GenericFragmentActivity.start(getActivity(), PhysicianConsultFragment.class, new Bundle());
    }


    public void healthyShoppingClick(View view) {
        GenericFragmentActivity.start(getActivity(), HealthyShopFragment.class, new Bundle());
    }

    //获取广告
    public void getData() {
        SendAdvertId send = new SendAdvertId();
        send.setAdvertId("5");
        send.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        ApiService.getAdvertInfo(getActivity(), new IHttpCallback<AdvertInfoBean>() {
            @Override
            public void onNext(AdvertInfoBean advertInfoBean) {
                initAdv(advertInfoBean);
            }

            @Override
            public void onError(int code, String message) {
                Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
            }
        }, Utils.getRequestBody(send));
    }

    public void initAdv(final AdvertInfoBean advertInfoBean) {
        if (isAdded()) {
            binding.slider.getPagerIndicator().setDefaultIndicatorColor(getActivity().getResources().getColor(R.color.transparent), Color.argb(1, 255, 255, 255));
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.slider.stopAutoCycle();
            }
        }, 200);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (advertInfoBean.getAdvert().size() <= 1) {
                    binding.slider.setBottomIndicator(View.GONE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
                } else {
                    binding.slider.setBottomIndicator(View.VISIBLE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Visible);
                    if (isAdded()) {
                        binding.slider.getPagerIndicator().setDefaultIndicatorColor(getActivity().getResources().getColor(R.color.physicalexamination_cycler_point), Color.argb(66, 255, 255, 255));
                    }
                }

                if (advertInfoBean.getAdvert().size() <= 1) {
                    binding.slider.stopAutoCycle();
                } else {
                    binding.slider.startAutoCycle();
                }
            }
        }, advertInfoBean.getAdvert().size() * 1000);

        List<String> img = new ArrayList<String>();
        List<String> imgurl = new ArrayList<String>();
        List<String> title = new ArrayList<String>();
        for (int i = 0; i < advertInfoBean.getAdvert().size(); i++) {
            img.add(i, advertInfoBean.getAdvert().get(i).getImage());
            imgurl.add(i, advertInfoBean.getAdvert().get(i).getUrl());
            title.add(i, advertInfoBean.getAdvert().get(i).getTitle());
        }

        TextSliderView textSliderView = new TextSliderView(getActivity(), img, imgurl, title);

        for (int i = 0; i < advertInfoBean.getAdvert().size(); i++) {
            // initialize a SliderLayout
            textSliderView
                    .image(img.get(i))
                    .setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                        @Override
                        public void onSliderClick(BaseSliderView slider) {

                        }
                    });

            binding.slider.addSlider(textSliderView);
        }
        binding.slider.setPresetTransformer(SliderLayout.Transformer.Stack);
        binding.slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        binding.slider.setCustomAnimation(new DescriptionAnimation());
    }
}
